function createStars() {
    const starsContainer = document.querySelector('.stars');
    const starCount = 100;

    for (let i = 0; i < starCount; i++) {
        const star = document.createElement('div');
        star.classList.add('star');
        star.style.left = `${Math.random() * 100}%`;
        star.style.top = `${Math.random() * 100}%`;
        star.style.animationDelay = `${Math.random() * 5}s`;
        starsContainer.appendChild(star);
    }
}
// 点点闪烁动画
createStars();

// 登录判断与保存信息
$(document).ready(function() {

    const $accountInput = $('#account');
    const $passwordInput = $('#password');
    const $rememberCheckbox = $('#remember-password');
    const $form = $('#myForm');
    const $submitButton = $('#submit');

    $form.submit(function(event) {
        event.preventDefault();
    });

    $submitButton.click(function() {
        const account = $accountInput.val();
        const password = $passwordInput.val();
        if ($rememberCheckbox.is(':checked')) {
            sessionStorage.setItem('account', account);
            sessionStorage.setItem('password', password);
        } else {
            sessionStorage.removeItem('account');
            sessionStorage.removeItem('password');
        }

        const upperCaseInput = input.value.toUpperCase();
        const upperCaseGeneratedCode = generatedCode.toUpperCase();

        if (upperCaseInput !== upperCaseGeneratedCode) {
            showInfo("验证码错误");
            generatedCode = draw();
            return;
        }

        $.ajax({
            // 请求
            url:"/login",
            method:"get",
            data: {account: account, password: password},
            // 响应
            dataType:"text",
            success: function(data) {
                if (data === "empty") {
                    showInfo('账号和密码不能为空！');
                    return;
                }
                if (data === "error") {
                    showInfo('账号或密码错误！');
                    generatedCode = draw();
                } else if (data === "HTTP/1.1 404 Not Found") {
                    showInfo('服务器错误！');
                } else if (data === "ban") {
                    showInfo('该账号已被封禁！');
                } else {
                    sessionStorage.setItem('nickname', data);
                    if(data === "Admin") {
                        window.location.href = "admin.html";
                    } else {
                        window.location.href = "index.html";
                    }
                }
            },
            error: function() {
                showInfo('服务器错误！');
            }
        });
    });

    $(window).ready(function() {
        const account = sessionStorage.getItem('account');
        const password = sessionStorage.getItem('password');
        if (account && password) {
            $accountInput.val(account);
            $passwordInput.val(password);
            $rememberCheckbox.prop('checked', true);
        }
    });
});

// 打开关闭弹窗
function openModal() {
    document.getElementById('modal').style.display = 'block';
    document.getElementById('overlay').style.display = 'block';
}

function closeModal() {
    document.getElementById('modal').style.display = 'none';
    document.getElementById('overlay').style.display = 'none';
}

// 修改密码（为了用户账号安全着想，不做实现）
function submitForm() {
    const currentPassword = document.getElementById('current-password').value;
    const newPassword = document.getElementById('new-password').value;
    const confirmPassword = document.getElementById('confirm-password').value;

    // 正则表达式判断
    const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/;

    if (!passwordRegex.test(newPassword)) {
        showInfo('新密码必须至少包含8个字符，且包含字母和数字。');
        return;
    }

    if (newPassword !== confirmPassword) {
        showInfo('新密码和确认密码不匹配。');
        return;
    }

    if (currentPassword === "" || newPassword === "" || confirmPassword === "") {
        showInfo("请填写完整！");
        return;
    }

    $.ajax({
        url:"/password",
        method: "get",
        data: {'type':"get", 'id': sessionStorage.getItem("id")},
        success: function (dataString) {
            const data = JSON.parse(dataString);
            if (data[0] === currentPassword) {
                $.ajax({
                    url:"/password",
                    method: "get",
                    data: {'type':"update", 'id': sessionStorage.getItem("userid"), password:newPassword},
                    success:function (dataString) {
                        if (dataString === "success") {
                            showInfo("修改成功");
                        } else {
                            showInfo("修改失败");
                        }
                    },
                    error: function (){
                        showInfo("服务器错误");
                    }
                })
            } else {
                showInfo("原密码错误");
            }
        },
        error: function (){
            showInfo("服务器错误");
        }
    })

    closeModal();
}